﻿@{
    ViewBag.Title = "Check lab";
}
@if (false)
{ <script src="../../Scripts/jquery-1.5.1.min.js" type="text/javascript"></script> }
<script src="http://code.jquery.com/ui/1.8.18/jquery-ui.min.js" type="text/javascript"></script>
<link href="http://code.jquery.com/ui/1.8.18/themes/redmond/jquery-ui.css" rel="stylesheet"
    type="text/css" />
<script type="text/javascript">
    var SUBJECT_SELECT_ID = "#subject_list";
    var TASK_SELECT_ID = "#task_list";
    var DESCRIPTION = "#description";
    var CHECK_FORM = "#check_form";
    var SELECTED_TASK = "#task_id";

    $(document).ready(function () {
        $.ajaxSetup({ cache: false });

        $(SUBJECT_SELECT_ID).change(function () {
            $.getJSON('@Url.Action("GetTasksForSubject", "Checker")' + "/" + $(this).val(), function (data) {
                $(TASK_SELECT_ID).find("option").remove();
                for (var i = 0; i < data.length; i++) {
                    $(TASK_SELECT_ID).append($(document.createElement("option")).attr("value", data[i].id).text(data[i].name));
                }
                $(TASK_SELECT_ID).change();
            });
        }).change();

        $(TASK_SELECT_ID).change(function () {
            $.getJSON('@Url.Action("GetTaskDescription", "Checker")' + "/" + $(this).val(), function (data) {
                $(DESCRIPTION).html(data);
            });
        });
        
        $("#check_button").click(function () {
            @if (Settings.Default.UseCheckerClient)
            {
            @: window.external.Check($("#task_list").val(), "cs");
            @: return false;                
            }
            else
            {
            @: if (!$("#file_upload").val()) {
            @:     alert('you must select a file');
            @:    return false;
            @: }            
            }
        });                 

        $("#check_button_cpp").click(function () {
            @if (Settings.Default.UseCheckerClient)
            {
            @: window.external.Check($("#task_list").val(), "cpp");
            @: return false;                
            }
            else
            {
            @: if (!$("#file_upload").val()) {
            @:     alert('you must select a file');
            @:    return false;
            @: }            
            }
        });   

        $(CHECK_FORM).submit(function () {
            $(SELECTED_TASK).val($(TASK_SELECT_ID).val());
            DisplayCheckStart();
        });
    });

    function DisplayCheckStart() {
        $("#checking_popup").html('<img src="@Url.Content("Content/check-progress.gif")" alt="Check in progress" style="display: block; margin: auto; margin-top: 20px;" />');
        $("#checking_popup").css("white-space", "pre-wrap").dialog({title: 'Checking...', width: 400, height: 200, modal: true });
    }

    function DisplayResults(number, result, message) {
        var numberLine = (number != undefined) ? ("Test #: " + number + "</br>") : "";
        $("#checking_popup").html(numberLine + (message ? ("</br>" + message) : "") + ((result == "Accepted") ? "</br> Все тесты пройдены!" : ""));
        $("#checking_popup").dialog({title: result, width: 400, height: 300, modal: true });
    }   
</script>
<div>
    @Html.DropDownList("subject_list", (IEnumerable<SelectListItem>)ViewBag.Subjects)
    <select id="task_list" name="task_list">
    </select>
    @using (Html.BeginForm("Check", "Checker", FormMethod.Post, new { id = "check_form", style = "margin-left: 30px; display: inline", enctype = "multipart/form-data", target = "upload_target" }))
    {
        if (!Settings.Default.UseCheckerClient)
        {
        @Html.Label("file_upload", "Program code file:")
        <input type="file" id="file_upload" name="file_upload" />
        }                
        <button type="submit" id="check_button">
            Проверить</button>
        
        <button type="submit" id="check_button_cpp">
            C++</button>
        
        @Html.Hidden("task_id")
    }
</div>
<div id="checking_popup" style="display: none">    
</div>
<div id="task_description" style="margin-top: 20px">
    <div id="description">
    </div>
</div>
<iframe id="upload_target" name="upload_target" src="#" style="width: 0px; height: 0px;
    border: none"></iframe>
